package ogdkp.command;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import ogdkp.View;
import ogdkp.om.Bank;
import ogdkp.om.ITreeNode;
import ogdkp.om.Loot;
import ogdkp.om.Player;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.PlatformUI;

public class ExportCommand extends AbstractHandler {

	private static final String DATABASE_DEFAULT = "yakitori_eqdkp";

	private final String PREFIX_DEFAULT = "eqdkp_";

	@Override
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		FileWriter fstream = null;
		BufferedWriter out = null;
		final File file = new File("dkp_" + System.currentTimeMillis() + ".xml");
		final View view =
		        (View) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("OGDKP.view");
		try {
			fstream = new FileWriter(file);
			out = new BufferedWriter(fstream);
			final List<Player> players = view.getModel();
			final String instance = view.getSelectedInstance();
			processDKP(out, players, instance);
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
					out = null;
				} catch (final IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (fstream != null) {
				try {
					fstream.close();
					fstream = null;
				} catch (final IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		MessageDialog.openInformation(view.getSite().getShell(), "Export Command", "Exported file successfully: "
		        + file.getAbsolutePath());
		return null;
	}

	private void processDKP(final BufferedWriter out, final List<Player> players, final String instance)
	        throws IOException {
		out.write("<MorgDKP2>");
		out.write("<version>2.1</version>");
		processRaid(out, players, instance);
		out.write("</MorgDKP2>");

	}

	private void processRaid(final BufferedWriter out, final List<Player> players, final String instance)
	        throws IOException {
		out.write("<Raids>");
		out.write("<key1>");
		out.write("<zone>" + instance + "</zone>");
		out.write("<date>" + getFormattedTime(System.currentTimeMillis()) + "</date>");
		out.write("<prefix>" + PREFIX_DEFAULT + "</prefix>");
		out.write("<eqdkp>" + DATABASE_DEFAULT + "</eqdkp>");
		// processAttendees(out, players);
		processBosskills(out, players, instance);
		processLoot(out, players);
		out.write("</key1>");
		out.write("</Raids>");

	}

	private void processLoot(final BufferedWriter out, final List<Player> players) throws IOException {
		int key = 0;
		out.write("<Loot>");
		for (final Player player : players) {
			if (player.getChildren().size() > 0) {
				for (final ITreeNode node : player.getChildren()) {
					final Loot loot = (Loot) node;
					out.write("<key" + ++key + ">");
					out.write("<ItemName>" + loot.getName() + "</ItemName>");
					if (loot.getCost() == 0) {
						out.write("<Costs>0.0</Costs>");
					} else {
						out.write("<Costs>" + loot.getCost() + "</Costs>");
					}
					out.write("<Player>" + player.getName() + "</Player>");
					out.write("<prefix>" + PREFIX_DEFAULT + "</prefix>");
					out.write("<eqdkp>" + DATABASE_DEFAULT + "</eqdkp>");
					out.write("<Boss>Trash mob</Boss>");
					out.write("<Time>" + getFormattedTime(loot.getTimestamp()) + "</Time>");
					out.write("</key" + key + ">");
				}
			}
		}
		out.write("</Loot>");
	}

	private void processBosskills(final BufferedWriter out, final List<Player> players, final String instance)
	        throws IOException {
		out.write("<Bosskills>");
		out.write("<key1>");
		out.write("<name>" + instance + "</name>");
		out.write("<note>X hours, X glass, X bosses, X bonus</note>");
		out.write("<time>" + getFormattedTime(System.currentTimeMillis()) + "</time>");
		out.write("<value>0.0</value>");
		out.write("<trash>2</trash>");
		out.write("<attendees>");
		final List<Player> raiders = getRaiders(players);
		for (int i = 1; i <= raiders.size(); i++) {
			out.write("<key" + i + ">" + raiders.get(i - 1).getName() + "</key" + i + ">");
		}
		out.write("</attendees>");
		out.write("</key1>");
		out.write("</Bosskills>");

	}

	private void processAttendees(final BufferedWriter out, final List<Player> players) throws IOException {
		out.write("<Attendees>");
		final List<Player> raiders = getRaiders(players);
		for (int i = 1; i <= raiders.size(); i++) {
			final Player player = raiders.get(i - 1);
			out.write("<key" + i + ">");
			out.write("<name>" + player.getName() + "</name>");
			out.write("<level>" + player.getLevel() + "</level>");
			out.write("<class>" + player.getClassName() + "</class>");
			out.write("<race>" + player.getRace() + "</race>");
			out.write("</key" + i + ">");
		}
		out.write("</Attendees>");
	}

	private static List<Player> getRaiders(final List<Player> players) {
		final List<Player> raiders = new ArrayList<Player>();
		for (final Player player : players) {
			if (player.isRaiding() && !(player instanceof Bank)) {
				raiders.add(player);
			}
		}
		return raiders;
	}

	private static String getFormattedTime(final long time) {
		// final String pattern = "MM/dd/yy HH:mm:ss";
		final DateFormat format = new SimpleDateFormat("MM/dd/yy HH:mm:ss");// DateFormat.getInstance();
		final String text = format.format(time);
		// System.out.println(text);
		return text;
	}
}
